import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import '../../share/my_assets_image.dart';
import '../../share/size_fill.dart';

//显示单张图片
class ShowPicWidget extends StatelessWidget {
  final double radius;
  final double width;
  final double? height;
  final String image;
  final BoxFit fit;
  final bool showDefault;

  BorderRadius? borderRadius;

  ShowPicWidget(
    this.image, {
    Key? key,
    this.radius = 5,
    this.width = 168,
    this.height,
    this.borderRadius,
    this.fit = BoxFit.cover,
    this.showDefault = true,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    borderRadius ??= BorderRadius.circular(radius);
    if (this.image.length == 0) {
      return ClipRRect(
        borderRadius: borderRadius,
        child: Image.asset(
          MyAssetsImages.defaultImage,
          width: width,
          height: height,
          fit: fit,
        ),
      );
    }
    return CachedNetworkImage(
      placeholder: !showDefault
          ? null
          : (ctx, url) {
              return Image.asset(
                MyAssetsImages.defaultImage,
                width: width,
                height: height,
                fit: fit,
              );
            },
      imageUrl: this.image.length == 0 ? "https://picsum.photos/200/200" : this.image,
      width: width,
      height: height,
      fit: fit,
    );
    return ClipRRect(
      borderRadius: borderRadius,
      child: FadeInImage.assetNetwork(
        placeholder: MyAssetsImages.defaultImage,
        image: this.image.length == 0 ? "https://picsum.photos/200/200" : this.image,
        width: width,
        height: height,
        fit: fit,
      ),
    );
  }
}
